package a09_贪心算法;

/**
 * <p>
 * a04_最大子序和复习1
 * </p>
 *
 * @author flyduck
 * @since 2025/1/10
 */
public class a04_最大子序和复习1 {
    //-2,1,-3,4,-1,2,1,-5,4
    //如何和为负数，抛弃
    public int maxSubArray(int[] nums) {
        int maxSum = Integer.MIN_VALUE;
        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            sum = sum + nums[i];
            maxSum = Math.max(maxSum, sum);
            if(sum < 0){
                sum = 0;
            }
        }
        return maxSum;
    }
}
